package com.ruoyi.statistics.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.LabStatisticsEquipment;
import com.ruoyi.statistics.service.ILabStatisticsEquipmentService;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.controller.LabCourseController;
import com.ruoyi.course.dto.CourseDto;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 贵重仪器设备Controller
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@RestController
@RequestMapping("/statistics/equipment")
public class LabStatisticsEquipmentController extends BaseController {
	@Autowired
	private ILabStatisticsEquipmentService labStatisticsEquipmentService;

	/**
	 * 查询贵重仪器设备列表
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:list')")
	@GetMapping("/list")
	public TableDataInfo list(LabStatisticsEquipment labStatisticsEquipment) {
		startPage();
		List<LabStatisticsEquipment> list = labStatisticsEquipmentService
				.selectLabStatisticsEquipmentList(labStatisticsEquipment);
		return getDataTable(list);
	}

	/**
	 * 导出贵重仪器设备列表
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:export')")
	@Log(title = "贵重仪器设备", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(LabStatisticsEquipment labStatisticsEquipment) {
		List<LabStatisticsEquipment> list = labStatisticsEquipmentService
				.selectLabStatisticsEquipmentList(labStatisticsEquipment);
		ExcelUtil<LabStatisticsEquipment> util = new ExcelUtil<LabStatisticsEquipment>(LabStatisticsEquipment.class);
		return util.exportExcel(list, "statisticsEquipment");
	}

	/**
	 * 获取贵重仪器设备详细信息
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(labStatisticsEquipmentService.selectLabStatisticsEquipmentById(id));
	}

	/**
	 * 新增贵重仪器设备
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:add')")
	@Log(title = "贵重仪器设备", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabStatisticsEquipment labStatisticsEquipment) {
		return toAjax(labStatisticsEquipmentService.insertLabStatisticsEquipment(labStatisticsEquipment));
	}

	/**
	 * 修改贵重仪器设备
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:edit')")
	@Log(title = "贵重仪器设备", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabStatisticsEquipment labStatisticsEquipment) {
		return toAjax(labStatisticsEquipmentService.updateLabStatisticsEquipment(labStatisticsEquipment));
	}

	/**
	 * 删除贵重仪器设备
	 */
	// @PreAuthorize("@ss.hasPermi('statistics:statisticsEquipment:remove')")
	@Log(title = "贵重仪器设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(labStatisticsEquipmentService.deleteLabStatisticsEquipmentByIds(ids));
	}

	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, Long baseCollegeId) throws Exception {
		ExcelUtil<LabStatisticsEquipment> util = new ExcelUtil<LabStatisticsEquipment>(LabStatisticsEquipment.class);
		List<LabStatisticsEquipment> quipmentDtos = util.importExcel(file.getInputStream());
		labStatisticsEquipmentService.importData(quipmentDtos, baseCollegeId);
		return AjaxResult.success("导入成功");
	}

	@GetMapping("/importTemplate")
	public void importTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 下载名称
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition",
				"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "基表三贵重仪器设备表(SJ3).xls"));
		InputStream resourceAsStream = LabCourseController.class.getClassLoader()
				.getResourceAsStream("imp_template/基表三贵重仪器设备表(SJ3)_template.xls");
		IOUtils.copy(resourceAsStream, response.getOutputStream());
	}
}
